const mongoose = require('mongoose');
const News = require('../model/news.model');


exports.get = function(req,res,next){
    const id = req.params.id;
    News.findById(id,function(err,data){
        res.json(data)
    })
}



/*新增*/
exports.create = function(req,res,next){
    const news = new News(req.body);

    news.save().then(data=>{
        res.json(data);
    });
}

/*修改*/
exports.update = function(req,res,next){
    const id = req.params.id;
   
    News.findByIdAndUpdate(id,{$set:req.body},{new:false})
    .then(data=>{
        res.json(data);
    });
}

/*删除*/
exports.remove = function(req,res,next){
    const id = req.params.id

    News.findByIdAndRemove(id,function(err,data){
        res.json({"message":"数据删除成功"});
    });
}

exports.removes = function(req,res,next){
    var ids = req.body.ids;
    ids = ids.split(',');
   
    if(ids.length>0){
        News.remove({_id:{$in:ids}})
        .then(data=>{
            res.json({"message":"多条数据删除成功"});
        });
    }else{
        res.status(404).send({"msg":"没有选中记录"})
    }
}

/*查找*/
exports.list = function(req,res,next){
    var page = (req.body.page) ? req.body.page : 1;
    var rows = (req.body.rows) ? req.body.rows : 5;

    var queryCondition = {};
 
    if(req.body.title && req.body.title.trim().length>0){
        title = req.body.title;
        queryCondition = {
            "title" : new RegExp(title,'i')
        }
    }
    if(req.body.cateId && req.body.cateId.trim().length>0){
        cateId = req.body.cateId;
        queryCondition = Object.assign(queryCondition,{
            cateId: cateId
        })
    }
    News.paginate(queryCondition,{page:+page,limit:+rows},function(err,result){
        result.rows=result.docs;
        delete result.docs;
        res.json(result);
    });
}